/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Administracion;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pato
 */
public class detalleSucursal extends javax.swing.JInternalFrame {

    Connection cn=local_connection.cn;
    PreparedStatement ps;
    ResultSet rs;
    int sucursal;
    Vector <Integer> idEncargado;
    int accion;
    Sucursales padre;
    public detalleSucursal(int tipo, int ide,Sucursales suc) {
        this.idEncargado = new Vector<Integer>();
        initComponents();
        encargado.addItem("Ninguno");
        idEncargado.add(0);
        id.setEnabled(false);
        padre=suc;
        sucursal=ide;
        accion=tipo;
        activo.setSelectedIndex(1);
        llenarEmpleados();
        if(accion==-1||accion==0)
            cargarDatos();
        if(accion==1)
            cargarId();
    }
    void cargarId(){
        try {
            ps=(PreparedStatement)cn.prepareStatement("Select id from sucursales order by id desc limit 1");
            rs=ps.executeQuery();
            rs.next();
            id.setText(String.valueOf(rs.getInt(1)+1));
        } catch (SQLException ex) {
            Logger.getLogger(detalleSucursal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    void llenarEmpleados(){
        try {
            ps=(PreparedStatement)cn.prepareStatement("SELECT concat(personas.nombre,' ',paterno,' ',materno),id_persona from personas where tipo = ? or tipo = ? ");
            ps.setString(1,"Empleado");
        ps.setString(2,"Usuario");
        rs=ps.executeQuery();
        while(rs.next()){
            encargado.addItem(rs.getObject(1));
            idEncargado.add(rs.getInt(2));
        }
        } catch (SQLException ex) {
            Logger.getLogger(detalleSucursal.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
void cargarDatos(){
        try {
            ps=(PreparedStatement)cn.prepareStatement("select id,sucursales.nombre,sucursales.direccion,sucursales.ciudad,sucursales.telefono,sucursales.cp,sucursales.pass,"
                    + "sucursales.activo,sucursales.estado,sucursales.notas,encargado from sucursales where id = ? ");
            ps.setInt(1, sucursal);
            rs=ps.executeQuery();
            rs.next();
            id.setText(rs.getString("id"));
            nombre.setText(rs.getString("sucursales.nombre"));
            direccion.setText(rs.getString("sucursales.direccion"));
            ciudad.setText(rs.getString("sucursales.ciudad"));
            telefono.setText(rs.getString("sucursales.cp"));
            clave.setText(rs.getString("sucursales.pass"));
            estado.setText(rs.getString("sucursales.estado"));
            activo.setSelectedIndex(rs.getInt("sucursales.activo"));
            cp.setText(rs.getString("sucursales.cp"));
            notas.setText(rs.getString("sucursales.notas"));
            encargado.setSelectedIndex(0);
            encargado.setSelectedIndex(idEncargado.indexOf(rs.getInt("encargado")));
            System.out.println(idEncargado.indexOf(rs.getInt("encargado")));
           /* for(int i=0;i<encargado.getItemCount();i++)
            notas.setText(rs.getString("sucursales.notas"));
            encargado.setSelectedIndex(0);
            encargado.setSelectedIndex(idEncargado.indexOf(
                if(encargado.getItemAt(i)==rs.getObject("concat(personas.nombre,' ',paterno,' ',materno)")){
                    encargado.setSelectedIndex(i);
                break;
                }*/
        } catch (SQLException ex) {
            Logger.getLogger(detalleSucursal.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(accion==-1)
            bloquearDetalle();
}
void bloquearDetalle(){
    nombre.setEnabled(false);
    direccion.setEnabled(false);
    ciudad.setEnabled(false);
    telefono.setEnabled(false);
    clave.setEnabled(false);
    estado.setEnabled(false);
    activo.setEnabled(false);
    cp.setEnabled(false);
    notas.setEnabled(false);
    encargado.setEnabled(false);
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        id = new javax.swing.JTextField();
        activo = new javax.swing.JComboBox();
        nombre = new javax.swing.JTextField();
        encargado = new javax.swing.JComboBox();
        clave = new javax.swing.JTextField();
        telefono = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        direccion = new javax.swing.JTextArea();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        notas = new javax.swing.JTextArea();
        ciudad = new javax.swing.JTextField();
        estado = new javax.swing.JTextField();
        cp = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Sucursal");

        jLabel1.setText("Id:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Direccion:");

        jLabel4.setText("Encargado:");

        jLabel5.setText("Ciudad:");

        jLabel6.setText("Estado:");

        jLabel7.setText("C.P.:");

        jLabel8.setText("Telefono:");

        jLabel9.setText("Clave:");

        jLabel10.setText("Activo:");

        id.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idActionPerformed(evt);
            }
        });

        activo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No", "Si" }));

        direccion.setColumns(20);
        direccion.setRows(5);
        jScrollPane1.setViewportView(direccion);

        jLabel11.setText("Notas:");

        notas.setColumns(20);
        notas.setRows(5);
        jScrollPane2.setViewportView(notas);

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel10)
                            .addComponent(jLabel2))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(id, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(activo, javax.swing.GroupLayout.Alignment.LEADING, 0, 168, Short.MAX_VALUE)
                            .addComponent(nombre, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel11)
                            .addGap(41, 41, 41)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel9)
                                .addComponent(jLabel4))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(encargado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(clave)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(telefono)
                    .addComponent(ciudad)
                    .addComponent(estado)
                    .addComponent(cp)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel10)
                                    .addComponent(activo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)))
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(encargado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(clave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(ciudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(estado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(cp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2))))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (accion==-1)
            this.dispose();
        else
        try {
        if(accion==0)
            ps=(PreparedStatement)cn.prepareStatement("update sucursales set activo = ?, ciudad = ?, pass = ?, cp = ?, direccion = ?, "
                    + "encargado = (select id_persona from personas where concat(personas.nombre,' ',paterno,' ',materno)= ?),estado= ? ,"
                    + "nombre = ?, notas = ?, telefono = ? where id = ?");
       if(accion ==1)
           ps=(PreparedStatement)cn.prepareStatement("insert into sucursales(activo,ciudad,pass,cp,direccion,encargado,estado,nombre,notas,telefono,id) "
                   + "values (?,?,?,?,?,(select id_persona from personas where concat(personas.nombre,' ',paterno,' ',materno)= ?),?,?,?,?,?)");
       ps.setInt(1,activo.getSelectedIndex());
       ps.setString(2, ciudad.getText());
       ps.setString(3, clave.getText());
       ps.setString(4, cp.getText());
       ps.setString(5, direccion.getText());
       ps.setString(6, String.valueOf(encargado.getSelectedItem()));
       ps.setString(7, estado.getText());
       ps.setString(8,nombre.getText());
       ps.setString(9, notas.getText());
       ps.setString(10,telefono.getText());
       ps.setString(11,id.getText());
       ps.execute();
       padre.cargarDatos();
       dispose();
        } catch (SQLException ex) {
            Logger.getLogger(detalleSucursal.class.getName()).log(Level.SEVERE, null, ex);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void idActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_idActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox activo;
    private javax.swing.JTextField ciudad;
    private javax.swing.JTextField clave;
    private javax.swing.JTextField cp;
    private javax.swing.JTextArea direccion;
    private javax.swing.JComboBox encargado;
    private javax.swing.JTextField estado;
    private javax.swing.JTextField id;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField nombre;
    private javax.swing.JTextArea notas;
    private javax.swing.JTextField telefono;
    // End of variables declaration//GEN-END:variables
}
